unit smGUIRender;

interface

type
  TGUIRender = class
  public
    procedure DrawGUI(const GUI: Pointer);
    procedure DrawPanel(const Panel: Pointer);
    procedure DrawPanelModal(const Panel: Pointer);
    procedure DrawButton(const Button: Pointer);
  end;

implementation

uses
  smGUI, smGUIPanel, smGUIButton, smMain, smUtils, smConst, smCommonTypes,
  SysUtils, Gen2;

{ TGUIRender }

procedure TGUIRender.DrawGUI;
begin
  with TGUI(GUI) do
    if Assigned(Background) then
      Game.Render2D.DrawRect(
        ClientRect.Left,
        ClientRect.Top,
        ClientRect.Width,
        ClientRect.Height,
        BackgroundColor, Background
      )
    else
    begin
      if BackgroundColor > $01000000 then
        Game.Prim2D.DrawRectHollow(
          ClientRect.Left + 1,
          ClientRect.Top + 1,
          ClientRect.Width - 1,
          ClientRect.Height - 1,
          BackgroundColor
        );
    end;
end;

procedure TGUIRender.DrawPanel;
var
  I: Integer;
  SelfPos: TGamePoint;
begin
  with TGUIPanel(Panel) do
  begin
    with SelfPos do
    begin
      X := Owner.ClientRect.Left + Left;
      Y := Owner.ClientRect.Top + Top;
    end;

    if Assigned(Background) then
      Game.Render2D.DrawRect(
        SelfPos.X, SelfPos.Y,
        Width, Height,
        CaseC(
            Byte(State) * Byte(Enabled),
            [ BackgroundColor,
              BackgroundColor,
              ColorBrighten(BackgroundColor, $08)
            ]
        ), Background{, 0, 1, 1, False, False, Width, Height,
        (Byte(Enabled) + Byte(State)) * Byte(Enabled)}
      )
    else
    begin
      if BackgroundColor > $01000000 then
        Game.Prim2D.DrawRect(
          SelfPos.X, SelfPos.Y, Width, Height,
          CaseC(
            Byte(State) * Byte(Enabled),
            [ BackgroundColor,
              BackgroundColor,
              ColorBrighten(BackgroundColor, $08)
            ]
          )
        );

      // Draw border
      if BorderColor > $01000000 then
        Game.Prim2D.DrawRectHollow(
          SelfPos.X, SelfPos.Y, Width, Height, BorderColor
        );
    end;


    if (Length(Caption) > 0) and Assigned(Font) and (FontColor > $00) then
      with TG2Font(Font) do
        Print(
          SelfPos.X + ClientRect.Left + ClientRect.Width div 2 -
          GetTextWidth(Caption) div 2,
          SelfPos.Y,
          FontColor, Caption
        );

    for I := 0 to ControlList.Count - 1 do
{$R-}
      with TGUIControl(ControlList[I]) do
        if Visible then
          Render(Self);
{$R+}
  end;
end;

procedure TGUIRender.DrawPanelModal;
begin
  with TGUIPanel(Panel) do
  begin
    if Owner.ModalColor > $01000000 then
      Game.Prim2D.DrawRect(0, 0, ScreenWidth, ScreenHeight, Owner.ModalColor);

    DrawPanel(Panel);
  end;
end;

procedure TGUIRender.DrawButton(const Button: Pointer);
var
  SelfPos: TGamePoint;
begin
  with TGUIButton(Button) do
  begin
    with Owner.AbsolutePos do
    begin
      SelfPos.X := X + Owner.ClientRect.Left + Left;
      SelfPos.Y := Y + Owner.ClientRect.Top + Top;
    end;

    if Assigned(Background) then
    begin
      // todo: draw textured button
      Game.Render2D.DrawRect(
        SelfPos.X, SelfPos.Y, Width, Height,
        BackgroundColor, Background, 0, 1, 1,
        False, False, Width, Height,
        Byte(Enabled) * MinOfB(Byte(State) + Byte(MouseDown) * 2, 3)
      );

      SelfPos.X := SelfPos.X + Byte(MouseDown);
      SelfPos.Y := SelfPos.Y + Byte(MouseDown);
    end
    else
    begin
      SelfPos.X := SelfPos.X + Byte(MouseDown);
      SelfPos.Y := SelfPos.Y + Byte(MouseDown);

      if BackgroundColor > $01000000 then
        Game.Prim2D.DrawRect(
          SelfPos.X, SelfPos.Y, Width, Height,
          CaseC(
            Byte(State) * Byte(Enabled),
            [ BackgroundColor,
              BackgroundColor,
              ColorBrighten(BackgroundColor, $08)
            ]
          )
        );

      if BorderColor > $01000000 then
        Game.Prim2D.DrawRectHollow(
          SelfPos.X, SelfPos.Y, Width, Height, BorderColor
        );
    end;

    if (Length(Caption) > 0) and Assigned(Font) then
      TG2Font(Font).Print(
        (SelfPos.X + Width div 2) - TG2Font(Font).GetTextWidth(Caption) div 2,
        (SelfPos.Y + Height div 2) - TG2Font(Font).GetTextHeight(Caption) div 2,
        FontColor, Caption
      );
  end;
end;

end.
